// pages/elderly/createActivity/createActivity.js
const app = getApp()
const { formatTime } = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    suggestsList:[],
    isOverdue: null,

    activity:{
      activitytitle: '',
      content: '',
      theme: null,
      createtime: null,
      statusflag: 0
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var activityType = options.activityType;
    var v = 'activity.theme'
    this.setData({
      [v]:activityType
    })
    console.log(this.data.activity)
  },
  onTitleInput: function (e) {
    var title = e.detail.value;
    var v = 'activity.activitytitle'
    this.setData({
      [v]: title
    })
  },
  onContentInput: function (e) {
    var content = e.detail.value;
    var v = 'activity.content'
    this.setData({
      [v]: content
    })
  },
  commitActivity: function (e) {
    var that = this
    var tempcontent = this.data.activity.activitytitle.trim()
    if(tempcontent==''|| tempcontent==null){
      wx.showToast({
        title: '标题不能为空',
        icon:'error'
      })
      return;
    }else {
      var time = formatTime(new Date())
      var vtime = 'activity.createtime'
      that.setData({
        [vtime]:time
      })
      console.log(that.data.activity)
      app.sendPostRequest('/heart/activity/insert',this.data.activity,(res)=>{
        console.log(res)
        if(res.code==200){
          wx.showToast({
            title: '发布成功',
            duration: 1500,
            success:function () {
              setTimeout(function(){
                var pages = getCurrentPages(); //当前页面
                var beforePage = pages[pages.length - 2]; //前一页
                beforePage.onLoad(); // 执行前一个页面的onLoad方法
                wx.navigateBack({
                  delta: 1
                });
              },1500)
            }
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})